package com.online.shop.controller;


import com.online.shop.common.SverResponse;
import com.online.shop.pojo.User;
import com.online.shop.service.ActionUserService;
import com.online.shop.utils.ConstUtil;
import com.online.shop.vo.ActionUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class ActionUserController {
	@Autowired
	private ActionUserService actionUserService;

	@RequestMapping("/login.do")
	@ResponseBody
	public SverResponse<User> doLogin(HttpSession session, String account, String password) {
		//1.调用Sevice登录方法
		SverResponse<User> response=actionUserService.doLogin(account, password);
		//2.判断返回结果
		if(response.isSuccess()){
			session.setAttribute(ConstUtil.CUR_USER, response.getData());
			}
		
		return response;

	}
	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	@RequestMapping( value="/do_register.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<String> registerUser(User user){
		return actionUserService.doRegister(user);
	}
	/**
	 * 验证用户并获得用户对象
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/getuserbyaccount.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<User> getUserByAccount(String account){
		return actionUserService.findUserByAccount(account);
	}
	/**
	 * 验证用户密码提示答案
	 * @param account
	 * @param password
	 * @param question
	 * @param asw
	 * @return
	 */
	@RequestMapping(value="/checkuserasw.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<String> checkUserAnswer(String account,String password,String question,String asw){
		return actionUserService.checkUserAnswer(account, question,asw);
	}
	/**
	 * 根据问题和答案重置密码
	 * @param userId
	 * @param newpwd
	 * @return
	 */
	@RequestMapping(value="/resetpassword.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<String> resetPassword(Integer userId,String newpwd){
		return actionUserService.resetPassword(userId,newpwd);
	}
	/**
	 * 修改用户个人资料
	 * @param session
	 * @param userVo
	 * @return
	 */
	@RequestMapping(value="/updateuserinfo.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<User> updateUserInfo(HttpSession session, ActionUserVo userVo){
		User curUser= (User) session.getAttribute(ConstUtil.CUR_USER);
		if(curUser==null){
			return SverResponse.createByErrorMessage("用户尚未登录！");
		}
		userVo.setId(curUser.getId());
		userVo.setAccount(curUser.getAccount());
		SverResponse<User> resp=actionUserService.updateUserInfo(userVo);
		if(resp.isSuccess()){
			//重写session
			session.setAttribute(ConstUtil.CUR_USER, resp.getData());
		}
		return resp;
	}
	/**
	 * 登录用户修改密码
	 * @param session
	 * @param newpwd
	 * @param oldpwd
	 * @return
	 */
	@RequestMapping(value="/updatepassword.do",method= RequestMethod.POST)
	@ResponseBody
	public SverResponse<String> updatePassword(HttpSession session, String newpwd, String oldpwd){
		//将session取出
		User user=(User) session.getAttribute(ConstUtil.CUR_USER);
		if(user==null){
			return SverResponse.createByErrorMessage("请登录后再修改密码");
		}
		SverResponse<String> result=actionUserService.updatePassword(user,newpwd,oldpwd);
		//修改成功后清空session准备重新登录
		if(result.isSuccess()){
			session.removeAttribute(ConstUtil.CUR_USER);
			
		}
		return  result;
	}
	
	@RequestMapping( value = "/do_check_info.do", method = RequestMethod.POST)
    @ResponseBody
    public SverResponse<String> checkValidUserInfo(String info, String type) {
        return actionUserService.checkValidation(info, type);
    }

    @RequestMapping( value = "/getuserinfo.do",method = RequestMethod.GET)
    @ResponseBody
    public SverResponse<User> getUserInfo(HttpSession session) {
        User user = (User)session.getAttribute(ConstUtil.CUR_USER);
        return user != null ? SverResponse.createRespBySuccess(user) : SverResponse.createByErrorMessage("无法获取用户信息！");
    }
    /**
	 * 用户登出
	 * @param session
	 * @return
	 */
	
	 @RequestMapping("/do_logout.do")
	 @ResponseBody
	 public SverResponse<String> loginOut(HttpSession session){
		 session.removeAttribute(ConstUtil.CUR_USER);
		 return SverResponse.createRespBySuccess();
	 }

}